<?php
/**
 * This file contains the class AuthorisationDB which provides
 * all of the database requirements for authorising a user
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 * @version    $Id:$
 */
/**
 * This class provides access to the database for MemberDB
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 * @version    Release: @package_version@
 */
class AuthorisationDB
{
    /**
      * Provides access to the database
     *
     * @var Db
     */
    private $_db;
    /**
     * Create a new instance of this class
     * 
     * @return void
     * @param Db $db
     */
    function __construct($db)
    {
        $this->_db = $db;
    }
    /**
         * Escape the provided value
         *
         * @return string
         * @param mixed $value
         */
    function escape($value)
    {
        $db = $this->_db;
        return $db->escape($value);
    }
    /**
     * Save an authentication code to the database
     *
     * @return int
     * @param string $authCode
     */
    function insertAuthCode($authCode)
    {
        $db = $this->_db;
        $sql = <<< EOT
        INSERT INTO
            Authentication (Code)
            VALUES ('%s')
EOT;
        $sql = sprintf($sql,
            $db->escape($authCode)
        );
        $db->query($sql);
        $authId = $db->insertID();
        return $authId;
    }
    /**
     * Update the user reference to an authentication id number
     *
     * @return bool
     * @param int $userId
     * @param int $authId
     */
    function setUserAuthId($userId, $authId)
    {
        $db = $this->_db;
        $sql = <<< EOT
        UPDATE Users
        SET AuthId = %d
        WHERE ID = %d
EOT;
        $sql = sprintf($sql,
            $db->escape($authId),
            $db->escape($userId)
        );
        $result = $db->query($sql);
        return $result === TRUE;
    }
    /**
     * Get an authentication code to the database
     *
     * @return string
     * @param int $userId
     */
    function getAuthCode($userId)
    {
        $db = $this->_db;
        $authCode = '';
        $sql = <<< EOT
        SELECT
            Code
        FROM
            Authentication
                INNER JOIN Users
                    ON Authentication.ID = Users.AuthId
        WHERE
            Users.ID = %d
EOT;
        $sql = sprintf($sql, $userId);
        $result = $db->query($sql);
        if ($db->rowCount($result) === 1) {
            $row = $db->fetch($result);
            $value = $row['Code'];
            $authCode = $db->escape($value);
        }
        return $authCode;
    }
    /**
     * Get the authentication time for the user
     *
     * @return string
     * @param int $userId
     */
    function getTime($userId)
    {
        $db = $this->_db;
        $time = '';
        $sql = <<< EOT
            SELECT Time
            FROM Authentication, Users
            WHERE
                Authentication.ID = Users.AuthID AND
                Users.ID = %d
              
EOT;
        $sql = sprintf($sql,
            $userId
        );
        $result = $db->query($sql);
        if ($db->rowCount($result) > 0) {
            $row = $db->fetch($result);
            $value = $row['Time'];
            $time = $db->unescape($value);
        }
        return $time;
    }
}
?>